package com.zheng.sms.weimi;

import com.aeye.mbr.common.exception.TalkeduException;
import com.aeye.mbr.common.util.StringUtils;
import com.aeye.mbr.upms.dao.model.SmsConfig;
import com.zheng.sms.rpc.api.SmsSender;
import com.zheng.sms.rpc.service.SmsConfigService;
import com.zheng.sms.rpc.service.SmsTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 微米短信服务
 * Created by chengshengru on 17-7-4.
 */
public class WeimiSmsSenderImpl implements SmsSender {


    private static Logger log = LoggerFactory.getLogger(WeimiSmsSenderImpl.class);


    private static final String DEFAULT_API_URL = "http://api.weimi.cc/2/sms/send.html";

    /**
     * 每天最大发送验证码数量
     */
    private static final int MAX_SMS_SEND_COUNT = 5;

    //@Autowired
    //private RedisUtil redisUtil;


    @Autowired
    private SmsConfigService smsConfigService;

    @Autowired
    private SmsTemplateService smsTemplateService;


    @Override
    public void sendTemplateSms(String mobile, String content) {

    }

    @Override
    public void sendTemplateSms(String mobile, LinkedHashMap<String, String> params) {

    }

    @Override
    public String sendVerifyCode(String mobile, int length) {
        return "";
    }

    @Override
    public boolean checkSmsCode(String mobile, String code, String type) {
        return false;
    }

    @Override
    public void clearSmsCode(String mobile, String type) {

    }





    public void clearSmsCode(Integer topOrgId,String mobile, String type){
        //String codeKey = StringUtils.format(BusinessCacheConstant.KEY_SMS_CODE_TYPE,topOrgId,mobile,type);
        //redisUtil.remove(codeKey);
    }


    private String buildMobileParam(String mobile) {
        StringBuilder builder = new StringBuilder();
        if (mobile.contains(",")) {
            String[] items = mobile.split(",");
            String item = null;
            boolean isFirst = true;
            for (int index = 0; index < items.length; index++) {
                item = items[index];
                if (StringUtils.isNotEmpty(item) && StringUtils.isMobile(item)) {
                    if (!isFirst) {
                        builder.append(",");
                    }
                    builder.append(item);
                    isFirst = false;
                } else {
                    log.error("手机号码格式不正确!参数为:" + mobile);
                    throw new TalkeduException("手机号码格式不正确!");
                }
            }
        } else {
            if (!StringUtils.isMobile(mobile)) {
                log.error("手机号码格式不正确!参数为:" + mobile);
                throw new RuntimeException("手机号码格式不正确!");
            }
            builder.append(mobile);
        }
        return builder.toString();
    }

    private Map<String, String> createRequestMap(Integer topOrgId,String mobile) {
        SmsConfig config = smsConfigService.getSmsConfig(topOrgId);
        if(config == null){
            throw new TalkeduException("该机构没有配置短信账号!");
        }
        Map<String, String> params = new HashMap<>();
        params.put("mob", buildMobileParam(mobile));
        params.put("uid", config.getAppId());
        params.put("type", "json");
        params.put("pas", config.getAppSecret());
        return params;
    }


}
